package cn.tedu._03vehicle.base.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.LocalDateTime;

//限定访问时间
@Slf4j
public class TimeAccessInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("限定访问时间拦截器preHandle方法执行");
        LocalDateTime now = LocalDateTime.now();
        if (now.getHour() < 8 || now.getHour() > 22) {
            log.debug("限定访问时间拦截器preHandle方法返回false");
            /*response.sendRedirect("http://www.baidu.com");
            return false;*/
            throw new RuntimeException("限定访问时间");
        }
        log.debug("限定访问时间拦截器preHandle方法返回true");
        return true;
    }
}
